// components/uploadImage/index.js
const { BASEURL } = require('../../utils/baseurl')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        imageNum: {
            type: Number,
            value: 9
        },
        uptype:{
          type:String,
          value:''
        },
        text:{
          type: String,
          value: ''
        },
        imageList: {
            type: Array,
            value: []
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
      tip:'init',
      show_tip:0,
      imagsetentlogs:[],
    },

    /**
     * 组件的方法列表
     */
    methods: {

        
upload() {
    const _this = this
    let { imageNum, imageList } = this.data
    let num = imageNum - imageList.length
    wx.chooseImage({
        count:  num <= 0 ? 0 : num,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
            var tempFilePaths = res.tempFilePaths;
            for (var i = 0; i < tempFilePaths.length; i++) {
                imageList.push(tempFilePaths[i]);
                this.setData({
                    imageList: imageList,
                    iscover:false,
                })
                           _this.setData({
                        tip:'已选图片,正在上传..',
                        show_tip:1,
                      })
                wx.uploadFile({
                    url:  `${BASEURL}wx/upload/image`,
                    filePath: res.tempFilePaths[i],
                    name: 'image',
                    formData: {
                    },
                    success: (res) => {
                      if(res.statusCode == 200) {
                        var resdata = JSON.parse(res.data);
                         _this.setData({
                          // imageList: _this.data.imageList.concat(resdata.data["url"]),
                          imagsetentlogs:_this.data.imagsetentlogs.concat(resdata.data.url),
                      })
                      //发送
                      _this.triggerEvent('upimages', {
                        imagsetentlogs: _this.data.imagsetentlogs
                      });
                      _this.setData({
                        tip: '显示',
                        show_tip: 0
                      })
                      }
                    },
                    fail: function(res) {
                    }
                })
            }
        }
    })
  },

        previewImage(e) {
            let index = e.target.dataset.index
            let { imageList } = this.data
            wx.previewImage({
                current: imageList[index],
                urls: imageList
            })
        },
        remove(e) {
            const _this = this
            let index = e.target.dataset.index
            let { imageList } = this.data
            wx.showModal({
                title: '提示',
                content: '您确定要删除本张图片吗？',
                success(res) {
                    if (res.confirm) {
                        imageList.splice(index, 1)
                        _this.setData({
                            imageList
                        })
                    } else if (res.cancel) {
                    }

                    //发送
                    _this.triggerEvent('upimages', {
                        imageList: _this.data.imageList
                    });


                }
            })
        }
    }
})
